package com.example.TheParent;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wonderfulradish.MainActivity;
import com.example.wonderfulradish.R;

import java.util.Timer;
import java.util.TimerTask;

public class TimeManager extends AppCompatActivity implements View.OnClickListener{

    private ImageView return_parent;
    private TextView setTime,restTime,changeTime;
    private TextView countTime;
    private Button GameButton,addTime,minusTime;
    private Timer timer = new Timer();
    private int gametime;
    private int resttime;
    private int counttime;
    private int changetime;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_manager);
        return_parent=(ImageView) findViewById(R.id.return_parent);
        return_parent.setOnClickListener(this);

        setTime=(TextView) findViewById(R.id.set_time);
        restTime=(TextView) findViewById(R.id.rest_time);
        countTime=(TextView) findViewById(R.id.count_time);
        addTime=(Button) findViewById(R.id.time_add);
        addTime.setOnClickListener(this);
        minusTime=(Button) findViewById(R.id.time_cacenl);
        minusTime.setOnClickListener(this);
        changeTime=(TextView) findViewById(R.id.change_time);

        gametime=Integer.parseInt(setTime.getText().toString());
        resttime=Integer.parseInt(restTime.getText().toString());
        counttime=Integer.parseInt(countTime.getText().toString());
        changetime=Integer.parseInt(changeTime.getText().toString());




        //生效按钮
        GameButton=(Button) findViewById(R.id.manager_start_game);
        GameButton.setOnClickListener(this);
    }


    TimerTask task2 = new TimerTask() {
        @Override
        public void run() {

            runOnUiThread(new Runnable() {      // UI thread
                @Override
                public void run() {

                    gametime--;
                    counttime++;
                    //时间格式
                    if (counttime<10){
                        countTime.setText("00:0"+counttime);
                    }else if (counttime>=10){
                        countTime.setText("00:"+counttime);
                    }
                    if(gametime<=10){
                        Toast.makeText(TimeManager.this, "还有5分钟注意把握时间", Toast.LENGTH_SHORT).show();;
                    }else if (gametime<=0){
                        Toast.makeText(TimeManager.this, "游戏时间到", Toast.LENGTH_SHORT).show();
                        AlertDialog.Builder timeOver=new AlertDialog.Builder(TimeManager.this);
                        timeOver.setIcon(R.drawable.jiasuo);
                        timeOver.setTitle("休息一会儿吧");
                        timeOver.setMessage("游戏时间结束了，快去休息一会儿吧");
                        timeOver.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                //退出游戏
                                //将等待游戏的线程打开
                            }
                  });
                        timeOver.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                //输入密码的界面
                                //进时间修改界面
                            }
                        });
                    }
                }
            });
        }
    };

    @Override
    public void onClick(View v){

        switch(v.getId()){
            case R.id.return_parent:
                finish();
                break;
            case R.id.manager_start_game:
                timer.schedule(task2,1000,1000);
                Toast.makeText(this, "你点击了生效按钮", Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(TimeManager.this, MainActivity.class);
                startActivity(intent);
                break;
            case R.id.time_add:
                changetime++;
                gametime++;
                resttime--;
                changeTime.setText(changetime);

                break;
            case R.id.time_cacenl:
                changetime--;
                gametime--;
                resttime++;
                changeTime.setText(changetime);
                break;
            default:
        }
    }

}
